﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using QLChoDiaOc.Process;

namespace QLChoDiaOc.Forms
{
    /// <summary>
    /// Interaction logic for FormCacQuanLyKhac.xaml
    /// </summary>
    public partial class FormCacQuanLyKhac : Page
    {
        ShowInfo _showInfo = new ShowInfo();
        UpdateInfo _updateInfo = new UpdateInfo();
        OtherHandle _otherHandle = new OtherHandle();

        static bool m_bSelectDataLoaiDiaOc = false;
        static bool m_bSelectDataLoaiBang = false;
        static bool m_bSelectDataToBao = false; 

        public FormCacQuanLyKhac()
        {
            InitializeComponent();
            AddComboBoxThuPhatHanh();
            _showInfo.ShowListLoaiDiaOc(dataLoaiDiaOc);
            _showInfo.ShowListLoaiBang(dataLoaiBang);
            _showInfo.ShowListToBao(dataToBao);
        }

        void AddComboBoxThuPhatHanh()
        {
            for (int i = 2; i <= 7; i++)
                thuPhatHanhComboBox.Items.Add("Thứ " + i.ToString());
            thuPhatHanhComboBox.Items.Add("Chủ nhật");
        }

        private void dataLoaiDiaOc_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!m_bSelectDataToBao && !m_bSelectDataLoaiBang && !m_bSelectDataLoaiDiaOc)
            {
                var item = dataLoaiDiaOc.SelectedItem as LOAIDIAOC;
                maLoaiDiaOcTextBox.Text = item.MaLoaiDiaOc;
                tenLoaiDiaOcTextBox.Text = item.TenLoaiDiaOc;

                //m_bSelectDataToBao = true;
                //m_bSelectDataLoaiBang = true;
                //m_bSelectDataLoaiDiaOc = true;
            }
            else
            {
                m_bSelectDataToBao = false;
                m_bSelectDataLoaiBang = false;
                m_bSelectDataLoaiDiaOc = false;
                _showInfo = new ShowInfo();
                _showInfo.ShowListLoaiDiaOc(dataLoaiDiaOc);
            }
        }

        private void dataLoaiBang_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!m_bSelectDataToBao && !m_bSelectDataLoaiBang && !m_bSelectDataLoaiDiaOc)
            {
                var item = dataLoaiBang.SelectedItem as LOAIBANG;
                maLoaiBangTextBox.Text = item.MaLoaiBang;
                tenLoaiBangTextBox.Text = item.TenLoaiBang;
                donGiaLoaiBangTextBox.Text = item.DonGia.ToString();

                //m_bSelectDataToBao = true;
                //m_bSelectDataLoaiBang = true;
                //m_bSelectDataLoaiDiaOc = true;
            }
            else
            {
                m_bSelectDataToBao = false;
                m_bSelectDataLoaiBang = false;
                m_bSelectDataLoaiDiaOc = false;
                _showInfo = new ShowInfo();
                _showInfo.ShowListLoaiBang(dataLoaiBang);
            }
        }

        private void dataToBao_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!m_bSelectDataToBao && !m_bSelectDataLoaiBang && !m_bSelectDataLoaiDiaOc)
            {
                var item = dataToBao.SelectedItem as TOBAO;
                maToBaoTextBox.Text = item.MaToBao;
                tenToBaoTextBox.Text = item.TenToBao;
                thuPhatHanhComboBox.SelectedItem = item.ThuPhatHanh.ToString();
                donGiaToBaoTextBox.Text = item.DonGia.ToString();

                //m_bSelectDataToBao = true;
                //m_bSelectDataLoaiBang = true;
                //m_bSelectDataLoaiDiaOc = true;
            }
            else
            {
                m_bSelectDataToBao = false;
                m_bSelectDataLoaiBang = false;
                m_bSelectDataLoaiDiaOc = false;
                _showInfo = new ShowInfo();
                _showInfo.ShowListToBao(dataToBao);
            }
        }

        private void btnSuaLoaiDiaOc_Click(object sender, RoutedEventArgs e)
        {
            if (tenLoaiDiaOcTextBox.Text == "")
                MessageBox.Show("Bạn chưa nhập đầy đủ thông tin", "Thông Báo");
            else
            {
                _updateInfo.UpdateLoaiDiaOc(maLoaiDiaOcTextBox.Text, tenLoaiDiaOcTextBox.Text);

                if (!m_bSelectDataLoaiDiaOc)
                    m_bSelectDataLoaiDiaOc = true;
                maLoaiDiaOcTextBox.Text = "";
                tenLoaiDiaOcTextBox.Text = "";
                dataLoaiDiaOc.SelectedItem = null;

                if (!m_bSelectDataLoaiBang)
                    m_bSelectDataLoaiBang = true;
                tenLoaiBangTextBox.Text = "";
                donGiaLoaiBangTextBox.Text = "";
                maLoaiBangTextBox.Text = "";
                dataLoaiBang.SelectedItem = null;

                if (!m_bSelectDataToBao)
                    m_bSelectDataToBao = true;
                tenToBaoTextBox.Text = "";
                donGiaToBaoTextBox.Text = "";
                thuPhatHanhComboBox.SelectedItem = null;
                maToBaoTextBox.Text = "";
                dataLoaiBang.SelectedItem = null;

                _showInfo = new ShowInfo();
                _showInfo.ShowListLoaiDiaOc(dataLoaiDiaOc);

                m_bSelectDataLoaiDiaOc = false;
                m_bSelectDataLoaiBang = false;
                m_bSelectDataToBao = false;
            }
        }

        private void btnSuaLoaiBang_Click(object sender, RoutedEventArgs e)
        {
            if (tenLoaiBangTextBox.Text == "" || donGiaLoaiBangTextBox.Text == "")
                MessageBox.Show("Bạn chưa nhập đầy đủ thông tin", "Thông Báo");
            else
            {
                _updateInfo.UpdateLoaiBang(maLoaiBangTextBox.Text, tenLoaiBangTextBox.Text,int.Parse(donGiaLoaiBangTextBox.Text));

                if (!m_bSelectDataLoaiDiaOc)
                    m_bSelectDataLoaiDiaOc = true;
                maLoaiDiaOcTextBox.Text = "";
                tenLoaiDiaOcTextBox.Text = "";
                dataLoaiDiaOc.SelectedItem = null;

                if (!m_bSelectDataLoaiBang)
                    m_bSelectDataLoaiBang = true;
                tenLoaiBangTextBox.Text = "";
                donGiaLoaiBangTextBox.Text = "";
                maLoaiBangTextBox.Text = "";
                dataLoaiBang.SelectedItem = null;

                if (!m_bSelectDataToBao)
                    m_bSelectDataToBao = true;
                tenToBaoTextBox.Text = "";
                donGiaToBaoTextBox.Text = "";
                thuPhatHanhComboBox.SelectedItem = null;
                maToBaoTextBox.Text = "";
                dataLoaiBang.SelectedItem = null;

                _showInfo = new ShowInfo();
                _showInfo.ShowListLoaiBang(dataLoaiBang);

                m_bSelectDataLoaiDiaOc = false;
                m_bSelectDataLoaiBang = false;
                m_bSelectDataToBao = false;
            }
        }

        private void btnSuaToBao_Click(object sender, RoutedEventArgs e)
        {
            if (tenToBaoTextBox.Text == "" || donGiaToBaoTextBox.Text == "" || thuPhatHanhComboBox.SelectedItem == null)
                MessageBox.Show("Bạn chưa nhập đầy đủ thông tin", "Thông Báo");
            else
            {
                _updateInfo.UpdateToBao(maToBaoTextBox.Text,tenToBaoTextBox.Text,thuPhatHanhComboBox.SelectedItem.ToString(),int.Parse(donGiaToBaoTextBox.Text));

                if (!m_bSelectDataLoaiDiaOc)
                    m_bSelectDataLoaiDiaOc = true;
                maLoaiDiaOcTextBox.Text = "";
                tenLoaiDiaOcTextBox.Text = "";
                dataLoaiDiaOc.SelectedItem = null;

                if (!m_bSelectDataLoaiBang)
                    m_bSelectDataLoaiBang = true;
                tenLoaiBangTextBox.Text = "";
                donGiaLoaiBangTextBox.Text = "";
                maLoaiBangTextBox.Text = "";
                dataLoaiBang.SelectedItem = null;

                if (!m_bSelectDataToBao)
                    m_bSelectDataToBao = true;
                tenToBaoTextBox.Text = "";
                donGiaToBaoTextBox.Text = "";
                thuPhatHanhComboBox.SelectedItem = null;
                maToBaoTextBox.Text = "";
                dataLoaiBang.SelectedItem = null;

                _showInfo = new ShowInfo();
                _showInfo.ShowListToBao(dataToBao);

                m_bSelectDataLoaiDiaOc = false;
                m_bSelectDataLoaiBang = false;
                m_bSelectDataToBao = false;
            }
        }

        private void btnThemLoaiDiaOc_Click(object sender, RoutedEventArgs e)
        {
            if (!m_bSelectDataLoaiDiaOc)
                m_bSelectDataLoaiDiaOc = true;
            maLoaiDiaOcTextBox.Text = "";
            tenLoaiDiaOcTextBox.Text = "";
            dataLoaiDiaOc.SelectedItem = null;
            WinLoaiDiaOc _winLoaiDiaOc = new WinLoaiDiaOc();
            _winLoaiDiaOc.Show();
        }

        private void btnThemLoaiBang_Click(object sender, RoutedEventArgs e)
        {
            if (!m_bSelectDataLoaiBang)
                m_bSelectDataLoaiBang = true;
            tenLoaiBangTextBox.Text = "";
            donGiaLoaiBangTextBox.Text = "";
            maLoaiBangTextBox.Text = "";
            dataLoaiBang.SelectedItem = null;
            WinLoaiBang _winLoaiBang = new WinLoaiBang();
            _winLoaiBang.Show();
        }

        private void btnThemToBao_Click(object sender, RoutedEventArgs e)
        {
            if (!m_bSelectDataToBao)
                m_bSelectDataToBao = true;
            tenToBaoTextBox.Text = "";
            donGiaToBaoTextBox.Text = "";
            thuPhatHanhComboBox.SelectedItem = null;
            maToBaoTextBox.Text = "";
            dataLoaiBang.SelectedItem = null;
            WinToBao _winToBao = new WinToBao();
            _winToBao.Show();
        }

        private void bntReloadLoaiDiaOc_Click(object sender, RoutedEventArgs e)
        {
            _showInfo = new ShowInfo();
            _showInfo.ShowListLoaiDiaOc(dataLoaiDiaOc);
        }

        private void bntReloadLoaiBang_Click(object sender, RoutedEventArgs e)
        {
            _showInfo = new ShowInfo();
            _showInfo.ShowListLoaiBang(dataLoaiBang);
        }

        private void bntReloadToBao_Click(object sender, RoutedEventArgs e)
        {
            _showInfo = new ShowInfo();
            _showInfo.ShowListToBao(dataToBao);
        }
    }
}
